<template>
  <section class="jumbotron">
    <h3 class="jumbotron-heading">Search Github Users</h3>
    <div>
      <input
        type="text"
        placeholder="enter the name you search"
        v-model="keyword"
      />&nbsp;
      <button @click="searchUsers">Search</button>
    </div>
  </section>
</template>

<script>
export default {
  name: "SearchV",
  data() {
    return {
      keyword: "",
    };
  },
  methods: {
    searchUsers() {
      if (this.keyword.trim() === "") {
        alert("输入为空，请重试！！！");
        return;
      }
      //请求前更新List信息
      // this.$bus.$emit("requestData", [], false, true, "");
      this.$bus.$emit("requestData", {
        usersList: [],
        isFirst: false,
        isLoading: true,
        errmsg: "",
      });

      //此处github后端使用cors设置了特殊响应头不用担心跨域问题
      this.$http //使用this.$http替换axios，功能与axios一样
        .get(`https://api.github.com/search/users?q=${this.keyword}`)
        .then(
          (res) => {
            //请求成功更新List信息
            // this.$bus.$emit("requestData", res.data.items, false, false, "");
            this.$bus.$emit("requestData", {
              usersList: res.data.items,
              isFirst: false,
              isLoading: false,
              errmsg: "",
            });
          },
          (err) => {
            //请求失败更新List信息
            // this.$bus.$emit("requestData", [], false, false, err.message);
            this.$bus.$emit("requestData", {
              usersList: [],
              isFirst: false,
              isLoading: false,
              errmsg: err.message,
            });
          }
        );
    },
  },
};
</script>

<style>
</style>